import os

from flask import request, render_template
from flask_cors import CORS
from flask_socketio import SocketIO

from app import create_app

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
CORS(app, resources={r"/*": {"origins": "*"}})  # 允许所有来源访问
app.config['JSON_AS_ASCII'] = False
@app.errorhandler(404)
def page_not_found(e):
    return render_template('errors/404.html'), 404

@app.before_request
def before():
    url = request.path  # 当前请求的URL
    passUrl = ["/login"]
    if url in passUrl:
        pass
    # else:
    #     return jsonify({'msg': '当前操作没有权限', 'code': 403})
    #     _id = session.get("_id", None)
    #     if not _id:
    #         return jsonify(401, {"msg": "认证失败，无法访问系统资源"})
    #     else:
    #         pass
if __name__ == '__main__':
    # 使用 SocketIO 启动应用
    socketio = SocketIO(app)
    socketio.run(app, host='0.0.0.0', port=8000, debug=True, allow_unsafe_werkzeug=True)